package com.burgeon.backend.SHOPBACKEND.controller;

import com.burgeon.backend.SHOPBACKEND.entity.Material;
import com.burgeon.backend.SHOPBACKEND.entity.MaterialEO;
import com.burgeon.backend.SHOPBACKEND.entity.MaterialGroupVo;
import com.burgeon.backend.SHOPBACKEND.entity.MaterialVo;
import com.burgeon.backend.SHOPBACKEND.mappers.MaterialMapper;
import com.burgeon.backend.SHOPBACKEND.service.MaterialService;
import com.burgeon.backend.stsutil.ResponseResult;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping("Material")
@RestController
@CrossOrigin
public class BackendMaterialController extends BaseController {

    @Autowired
    private MaterialService materialService;



    /**
     * 新增素材分组
     * @param file 封面
     * @param groupName 名称
     * @param account account
     * @return 修改0或1
     */
    @RequestMapping("addGroup")
    public ResponseResult<Integer> addMaterialGroup(@RequestParam(value = "img",required = false)MultipartFile file,
                                                    String groupName, String account){

        System.err.println(file);
        Integer data = materialService.insertMaterialGroup(file,groupName,account);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 查看素材分组
     * @param account account
     * @return 分组
     */
    @RequestMapping("findGroupByAccount")
    public ResponseResult<MaterialGroupVo> findMaGroup(String account){

        MaterialGroupVo data = materialService.findGroupByAccount(account);

        return new ResponseResult<>(SUCCESS,data);
    }



    /**
     * 搜索素材分组
     * @param account account
     * @return 分组
     */
    @RequestMapping("findGroupByGroupName")
    public ResponseResult<MaterialGroupVo> findGroupByGroupName(String account,String groupName){

        MaterialGroupVo data = materialService.findGroupByName(account,groupName);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 删除分组
     * @param ids
     * @param account
     * @return
     */
    @RequestMapping("deMaterialGroup")
    public ResponseResult<Integer> deMaterialGroup(String ids, String account) throws JSONException {

        materialService.deGroupById(ids,account);

        return new ResponseResult<>(SUCCESS);
    }



    /**
     * 修改分组
     * @param id
     * @param groupName
     * @return
     */
    @RequestMapping("upGroup")
    public ResponseResult<Integer> upGroup(@RequestParam(value = "id")String id,
                                           @RequestParam(value = "groupName")String groupName,
                                           @RequestParam(value = "img")MultipartFile file,
                                           @RequestParam(value = "account")String account){

       Integer data = materialService.upGroupName(id,groupName,file,account);

       return new ResponseResult<>(SUCCESS,data);
    }


    /**=================================素材===================================================*/

    /**
     * 新增素材
     * @param account account
     * @return 分组
     */
    @RequestMapping("addMaterial")
    public ResponseResult<Integer> insertMaterial(
            @RequestParam(value = "img")MultipartFile[] files,
                                                  String materialName, String groupId,
                                                  String imgDescribe,
                                                  String account,String price){

        Integer data = materialService.insertMaterial(files, materialName, groupId, imgDescribe, account,price);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 修改素材
     * @return
     */
    @RequestMapping("upMaterial")
    public ResponseResult<Integer> upMaterial(String materialName,String groupId,String imgDescribe,String id,String price){

        Integer data = materialService.upMaterial(materialName,groupId,imgDescribe,id,price);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 删除素材
     * @return
     */
    @RequestMapping("deleteMaterial")
    public ResponseResult<Integer> deleteMaterial(String ids,String account) throws JSONException {

        materialService.deMaterialById(ids,account);

        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 删除素材图片
     * @return
     */
    @RequestMapping("deleteMaterialImg")
    public ResponseResult<Integer> deleteMaterialImg(String id,String account){

        Integer data = materialService.deMateriaImglById(id,account);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 修改素材图片增
     * @param mid
     * @param file
     * @param account
     * @return
     */
    @RequestMapping("addMaterialImg")
    public ResponseResult<Integer> addMaterialImg(
            String mid,
            @RequestParam(value = "img")MultipartFile file,
            String account){


        Integer data = materialService.insertMaterialImgs(mid, file, account);
        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 根据素材id查询素材内容
     * @param id
     * @return
     */
    @RequestMapping("getMaterialById")
    public ResponseResult<MaterialVo> getMaterialById(String id){

        MaterialVo data = materialService.findMaterialById(id);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 获取所有素材
     * @param account
     * @return
     */
    @RequestMapping("getMaterialAll")
    public ResponseResult<MaterialEO> getMaterialAll(String account,int page){

        MaterialEO data = materialService.getMaterialAll(account,page);

        return new ResponseResult<>(SUCCESS,data);
    }



    /**
     * 搜索素材
     * @param account
     * @return
     */
    @RequestMapping("findWebMaterialByName")
    public ResponseResult<MaterialEO> findWebMaterialByName(String account,String materialName,int page,String groupId){

        MaterialEO data = materialService.findWebMaterialByName(account,materialName,page,groupId);

        return new ResponseResult<>(SUCCESS,data);
    }


    /**=================================素材图片连接=========================================*/

    /**
     * 添加图片链接
     * @param miid
     * @param gid
     * @return
     */
    @RequestMapping("addMaterialLink")
    public ResponseResult<Integer> addMaterialLink(String miid, String gid){

        Integer data = materialService.addMaterialLink(miid,gid);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 获取链接
     * @param id
     * @return
     */
    @RequestMapping("getLink")
    public ResponseResult<String> getLink(String id){

        String data = materialService.findMaLinkById(id);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 修改图片链接
     * @param mid
     * @param gid
     * @return
     */
    @RequestMapping("upMaLink")
    public ResponseResult<Integer> upMaLink(String mid, String gid){
        Integer data = materialService.upMaLinkByMid(mid,gid);
        return new ResponseResult<>(SUCCESS,data);
    }


    /**
     * 根据id链接获取素材
     * @param id
     * @return
     */
    @RequestMapping("getGoodsById")
    public ResponseResult<Material> getGoodsById(String id,String daoId){

        System.err.println(daoId);
        System.err.println(id);
//        MaterialGoods data = materialService.findGoodsById(id);
//        System.err.println(data);
        Material material = materialService.getMaterialById(id);

        return new ResponseResult<>(SUCCESS,material);
    }


}
